var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      nickName: '点击登录',
      avatarUrl: '/static/images/avatar.png'
    },
    order: {
      unpaid: 0,
      unship: 0,
      unrecv: 0,
      uncomment: 0
    },
    MyMenus: [{
        url: "/pages/groupon/myGroupon/myGroupon",
        pic: "group.png",
        name: "我的拼团"
      },
      {
        url: "/pages/ucenter/address/address",
        pic: "address.png",
        name: "收货地址"
      },
      {
        url: "/pages/ucenter/footprint/footprint",
        pic: "footprint.png",
        name: "我的足迹"
      },
      {
        url: "/pages/ucenter/feedback/feedback",
        pic: "feedback.png",
        name: "意见反馈"
      },
      {
        url: "/pages/about/about",
        pic: "about_us.png",
        name: "关于我们"
      }
    ],
    hasVip:0,//是否vip用户可核销功能
    hasLogin: false
  },

  goPages: function (e) {
    if (this.data.hasLogin) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },

  /**
   * 跳转到团长中心
   */
  goLeaderCorePages: function () {
    wx.navigateTo({
      url: "/pages/ucenter/leaderCore/index"
    });
  },
  onLoad: function (options) {},
  onHide: function () {},
  onUnload: function () {},
  onShow: function () {
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      this.setData({
        userInfo: userInfo,
        hasLogin: true
      });
    }

    let that = this;
    if (app.globalData.hasLogin) {
      util.request(api.UserIndex).then(function (res) {
        if (res.errno == 0) {
          that.setData({
            hasVip: res.data.userType,
            order: res.data.order,
            remainAmount: res.data.remainAmount,
            couponCount: res.data.couponCount,
            integral: res.data.integral
          });
        }
      });
    }
  },
  onUnload: function () {},
  goLogin() {
    if (!this.data.hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goBrokerage() {
    if (this.data.hasLogin) {
      wx.navigateTo({
        url: "/pages/brokerage/main/main"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  goOrder() {
    if (this.data.hasLogin) {
      try {
        wx.setStorageSync('tab', '0');
      } catch (e) {}
      wx.navigateTo({
        url: "/pages/ucenter/order/order"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goOrderRefund() {//售后退款
    if (this.data.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/orderRefund/orderRefund"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goOrderIndex(e) {
    if (this.data.hasLogin) {
      let tab = e.currentTarget.dataset.index
      let route = e.currentTarget.dataset.route
      try {
        wx.setStorageSync('tab', tab);
      } catch (e) {}
      wx.navigateTo({
        url: route,
        success: function (res) {},
        fail: function (res) {},
        complete: function (res) {},
      })
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  getScancode:function(){//核销用户
    var _this = this;
    wx.scanCode({
      success: (res) => {
        var result = res.result;
        if(result){
          util.request(api.VerificateUser, {
            content: result
          }).then(function (res) {
            util.showInfo(res.data);
          });
        }
      }
    })
  }
})